'use strict';

app.controller('logCtrl', ["$scope", "i18nService", "$rootScope", "$timeout", "$state", "$modal", "SweetAlert", "uiGridConstants", "uiGridGroupingConstants", "Restangular", "api_configure_data", function($scope, i18nService, $rootScope, $timeout, $state, $modal, SweetAlert, uiGridConstants, uiGridGroupingConstants, Restangular, api_configure_data) {
    $scope.langs = i18nService.getAllLangs();
    $scope.lang = 'zh-cn';
    i18nService.setCurrentLang($scope.lang);

    var loginUser = $rootScope.user;
    $scope.gridOptions = {};
    $scope.gridOptions.data = 'myData';
    $scope.gridOptions.enableColumnResizing = true;
    $scope.gridOptions.enableFiltering = true;
    $scope.gridOptions.enableGridMenu = false;
    $scope.gridOptions.enableRowSelection = true;
    $scope.gridOptions.showGridFooter = true;
    $scope.gridOptions.showColumnFooter = true;
    $scope.gridOptions.fastWatch = true;
    $scope.gridOptions.useExternalFiltering = true;
    $scope.gridOptions.useExternalPagination = true;
    $scope.gridOptions.paginationPageSizes = [10];
    $scope.gridOptions.paginationPageSize = 10;
    $scope.gridOptions.multiSelect = false;


    $scope.gridOptions.rowIdentity = function(row) {
        return row.id;
    };
    $scope.gridOptions.getRowIdentity = function(row) {
        return row.id;
    };

    $scope.gridOptions.columnDefs = [
        { name: 'operationIdentification', displayName: '搜索代码', width: 140, enableFiltering: false },
        { name: 'operationType.code', displayName: '操作类型', width: 120, enableFiltering: false },
        { name: 'operationUser.name', displayName: '操作人', width: 120, enableFiltering: false },
        { name: 'operationTime', displayName: '操作时间', width: 160, enableFiltering: false }
    ];

    $scope.refresh = function(style, filterData) {
        var filtersData = {
            idx: 0,
            sum: 10
        };
        $scope.gridOptions.refresh();
        $scope.refreshData('expand-right', filtersData);
    };
    $scope.open = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened = !$scope.opened;
    };
    $scope.endOpen = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.startOpened = false;
        $scope.endOpened = !$scope.endOpened;
    };
    $scope.startOpen = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.endOpened = false;
        $scope.startOpened = !$scope.startOpened;
    };
    $scope.chicecmdb = function(parameters, key) {
        var fildata = defaultFilterData;
        $scope.memoryfilterData = fildata;
        if (!fildata.operationLog) {
            fildata['operationLog'] = {};
        }
        $scope.gridOptions.paginationCurrentPage = 1;
        if (parameters) {
            angular.extend(fildata, { 'idx': 0, 'sum': $scope.gridOptions.paginationPageSize })
            angular.extend(fildata.operationLog, { 'operationTime': moment(parameters.paramDateFrom).format('YYYY-MM-DD HH:mm:ss'), 'endOperationTime': moment(parameters.paramDateTo).format('YYYY-MM-DD 23:59:59'), 'operationUserName': key })
        }
        $scope.refreshData('expand-right', fildata);
    }
    $scope.gridOptions.onRegisterApi = function(gridApi) {
        $scope.gridApi = gridApi;
        gridApi.pagination.on.paginationChanged($scope, function(newPage, pageSize) {
            var filtersData = $scope.memoryfilterData;
            filtersData.idx = newPage - 1;
            filtersData.sum = pageSize;
            $rootScope['idx'] = newPage;
            $scope.refreshData('expand-right', filtersData);
            // $scope.refreshData('expand-right', {"assignee":$rootScope.user.id, "candidateGroups":$rootScope.user.group[0].id, "idx":newPage-1,"sum":pageSize});                                            
        });

        gridApi.selection.on.rowSelectionChanged($scope, function(data) {
            //$scope.selected.item = data.entity;
            // console.log(data);
        });
        gridApi.core.on.filterChanged($scope, function() {
            var grid = this.grid;
            var filtersData = {
                idx: 0,
                sum: 10
            };
            angular.forEach(grid.columns, function(item) {
                if (item.enableFiltering) {
                    if (angular.isDefined(item.filters[0].term) && item.filters[0].term != '') {
                        // console.log('item.filters[0]='+JSON.stringify(item.filters[0]));
                        if (angular.isUndefined(filtersData['incident'])) {
                            filtersData['incident'] = {};
                        }
                        filtersData['incident'][item.field] = item.filters[0].term;
                    }
                }
            });
            $scope.memoryfilterData = filtersData;
            $scope.refreshData('expand-right', filtersData);
        });
    };
    $scope.memoryfilterData = defaultFilterData = {
        "idx": 0,
        "sum": 10
    };




    //$scope.callsPending = 0;
    //var i = 0;
    var defaultFilterData = {
        "idx": 0,
        "sum": 10
    };

    $scope.ldloading = {};

    $scope.refreshData = function(style, filterData) {
        $scope.ldloading[style.replace('-', '_')] = true;
        if (angular.isUndefined(filterData)) {
            filterData = defaultFilterData;
        }
        $scope.myData = [];
        api_configure_data.fetchDataList("operationLog", filterData).then(function(data) {
            var myData = Restangular.stripRestangular(data);
            $scope.gridOptions.totalItems = myData.resultCount;
            $scope.myData = myData.list;
            // for (var i = 0; i < $scope.myData.length; i++) {
            //     $scope.myData[i].id = i;
            // }
            $scope.ldloading[style.replace('-', '_')] = false;
        }, function() {
            $scope.ldloading[style.replace('-', '_')] = false;
        });
    };

    $scope.refreshData('expand-right', defaultFilterData);
}]);